package com.surfilter.massdata.es.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtils {
	
	private static final Log log = LogFactory.getLog(DateUtils.class);
	
	public static final String DB_TABLE_SUFFIX_DATE_FORMAT="yyyyMMdd";
	public static final String FILE_PATH_DATE_FORMAT="yyyy/MM/dd";
	public static final String LONG_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String ES_INDEX_FORMAT = "yyyy_MM_dd"; 
	public static final String ES_MONTH_INDEX_FORMAT = "yyyyMM";
	public static final String ES_MONTH_INDEX_FORMAT_TP = "yyyyMMdd";
	
	
	public static String formatDateToES(Date date){
		return formatDate(date,ES_INDEX_FORMAT);
	}
	
	public static String formatDateTotpES(Date date){
		return formatDate(date,ES_MONTH_INDEX_FORMAT_TP);
	}
	
	public static String formatMonthToES(Date date){
		return formatDate(date,ES_MONTH_INDEX_FORMAT);
	}
	
	public static String formatDate(Date value,String format){
		String date = "";
		try{
			DateFormat formatter = new SimpleDateFormat(format);
			date = formatter.format(value);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return date;
	}
	
	public static Date esToformatDate(String value){
		DateFormat formatter = new SimpleDateFormat(ES_INDEX_FORMAT);
		Date date = null;
		try {
			date = formatter.parse(value);
		} catch (ParseException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return date;
	}

	public static String getCurrentDayStr(){
		Date currentDate = Calendar.getInstance().getTime();
		return formatDate(currentDate,DB_TABLE_SUFFIX_DATE_FORMAT);
	}

	public static String getDBDayFormatStr(Date date){
		return formatDate(date,DB_TABLE_SUFFIX_DATE_FORMAT);
	}


	public static String getCurrentDayFilePathStr(){
		Date currentDate = Calendar.getInstance().getTime();
		return formatDate(currentDate,FILE_PATH_DATE_FORMAT);
	}

	public static String getYesterdayDayFilePathStr(){
		Date yesterday = getYesterDay();
		return formatDate(yesterday,FILE_PATH_DATE_FORMAT);
	}


	public static Date getYesterDay() {
		Calendar ca= Calendar.getInstance();
		ca.add(Calendar.DAY_OF_MONTH, -1);
		Date yesterday=ca.getTime();
		return yesterday;
	}

	public static Date stringToDBFormatDate(String value){
		DateFormat formatter = new SimpleDateFormat(DB_TABLE_SUFFIX_DATE_FORMAT);
		Date date = null;
		try {
			date = formatter.parse(value);
		} catch (ParseException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return date;
	}

	public static Date stringToLongFormatDate(String value){
		DateFormat formatter = new SimpleDateFormat(LONG_DATE_FORMAT);
		Date date = null;
		try {
			date = formatter.parse(value);
		} catch (ParseException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return date;
	}

	public static Date stringToDate(String value){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = formatter.parse(value);
		} catch (ParseException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return date;
	}

	 public static Date getUTCTime(Date date){
        //Calendar calendar = Calendar.getInstance();
        TimeZone zone = TimeZone.getTimeZone("GMT");
        Calendar calendar = new GregorianCalendar(zone);

        calendar.setTimeInMillis(date.getTime());
        calendar.add(Calendar.HOUR, 8);
       // calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
        return calendar.getTime();
    }

}
